<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">



        <p-form-item label="物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'materialNo', validatorRules.materialNo]" placeholder="请输入物料编号"></p-input>
        </p-form-item>

        <p-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'materialName', validatorRules.materialName]" placeholder="请输入物料名称"></p-input>
        </p-form-item>

        <p-form-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'specifications', validatorRules.specifications]" placeholder="请输入规格型号"></p-input>
        </p-form-item>

        <p-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'unit', validatorRules.unit]" placeholder="请输入单位"></p-input>
        </p-form-item>

        <p-form-item label="类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'materialType', validatorRules.materialType]" placeholder="请输入类别"></p-input>
        </p-form-item>

        <p-form-item label="来源" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['source', validatorRules.source]" :trigger-change="true" dictCode="material_source" placeholder="请选择来源"/>
        </p-form-item>

        <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
        </p-form-item>


      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
  import { httpAction, putAction, api } from '@/api/manage'

  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "FxMaterialModal",
    components: { 
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          materialNo: {rules: [
          ]},
          materialName: {rules: [
          ]},
          specifications: {rules: [
          ]},
          unit: {rules: [
          ]},
          materialType: {rules: [
          ]},
          source: {rules: [
          ]},
          remark: {rules: [
          ]},
        },
        url: {
          add: '/codedemoapi/codedemo/fxMaterial/add',
          edit: '/codedemoapi/codedemo/fxMaterial/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
        this.getRuleCodeValue();
      },
      getRuleCodeValue() {
        let param = {
          commonFormData: this.model,
          rules: [
            {"ruleCode": "category_code_rule1", "columnName": "materialNo"},
            {"ruleCode": "category_code_rule2", "columnName": "materialName"},
            ]
        }

        putAction(api.ruleBaseURL, param).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            console.log("生成的编码：" + res.data)
            const item = res.data;
            this.form.setFieldsValue(pick(item, Object.keys(item)))
          }
        })
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'materialNo','materialName','specifications','unit','materialType','source','remark'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.msg);
                that.$emit('ok');
              }else{
                that.$message.warning(res.msg);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'materialNo','materialName','specifications','unit','materialType','source','remark'))
      },

      
    }
  }
</script>